package com.atguigu.flink.chapter06;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.Date;

public class WindowDemo2 {
        public static void main(String[] args) {
                Configuration conf = new Configuration();
                conf.setInteger("rest.port", 2000);
                StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
                env.setParallelism(1);

                env.socketTextStream("hadoop102",8888)
                        .flatMap(new FlatMapFunction<String, Tuple2<String,Integer>>() {
                                @Override
                                public void flatMap(String value,
                                                    Collector<Tuple2<String, Integer>> out) throws Exception {
                                        String[] words = value.split(" ");
                                        for (String word : words) {
                                                out.collect(Tuple2.of(word,1));
                                        }
                                }
                        })
                        .keyBy(t -> t.f0)
                        //定义窗口：基于处理时间的滑动窗口，窗口长度：5s ,窗口的步长：2s
                        .window(SlidingProcessingTimeWindows.of(Time.seconds(5),Time.seconds(2)))
                        //窗口处理函数： 如何处理窗口中的元素
                        /*
                        参数1 : 输入
                        参数2 ：输出
                        参数3 ：Key的类型
                        参数4 ： 窗口的类型
                         */
                        .process(new ProcessWindowFunction<Tuple2<String, Integer>, String, String, TimeWindow>() {
                                @Override
                                public void process(String key,  //窗口属于的key
                                                    Context ctx,  //上下文对象： 获取窗口的开始和结束
                                                    Iterable<Tuple2<String, Integer>> elements,   //集合：存储当前窗口的所有元素
                                                    Collector<String> out) throws Exception {
                                        ArrayList<String> list = new ArrayList<>();
                                        for (Tuple2<String, Integer> t : elements) {
                                                list.add(t.f0 + "_" + t.f1);
                                        }
                                        String msg = new Date(ctx.window().getStart()) + "," + new Date(ctx.window().getEnd()) + "," + list;
                                        out.collect(msg);
                                }
                        })
                        .print();

                try {
                        env.execute();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}
